package com.liao.seckill.controller;

import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.liao.seckill.constant.UserConstant;
import com.liao.seckill.dto.UserDto;
import com.liao.seckill.pojo.TbUser;
import com.liao.seckill.result.Result;
import com.liao.seckill.result.ResultCode;
import com.liao.seckill.service.UserService;
import com.liao.seckill.vo.ResUser;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Base64;
import java.util.HashMap;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Controller
@RequestMapping("/auth")
public class AuthController {

    private UserService userService;
    private DefaultKaptcha defaultKaptcha;
    private StringRedisTemplate redisTemplate;

    public AuthController(UserService userService, DefaultKaptcha defaultKaptcha, StringRedisTemplate redisTemplate) {
        this.userService = userService;
        this.defaultKaptcha = defaultKaptcha;
        this.redisTemplate = redisTemplate;
    }

    @ResponseBody
    @RequestMapping(value = "/captcha", method = RequestMethod.GET)
    public Result<Object> verificationCode() throws IOException {
        String text = defaultKaptcha.createText();
        BufferedImage image = defaultKaptcha.createImage(text);
        String s = UUID.randomUUID().toString();
        redisTemplate.opsForValue().set(UserConstant.VERIFICATION_CODE_KEY + ":" + s,
                text, UserConstant.CAPTCHA_EXPIRED_TIME, TimeUnit.SECONDS);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ImageIO.write(image, "jpg", baos);
        Base64.Encoder encoder = Base64.getEncoder();
        HashMap<String, Object> map = new HashMap<>();
        map.put("captcha", encoder.encodeToString(baos.toByteArray()));
        map.put("capKey", s);
        System.out.println("data:image/jpeg;base64," + encoder.encodeToString(baos.toByteArray()));
        return new Result<>(map);
    }

    @ResponseBody
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public Result<ResUser> login(UserDto userDto) {
        System.out.println(userDto);
        if (userDto == null) {
            return new Result<>(ResultCode.PARAMETER_ERROR);
        }
        if (userDto.getCaptcha() == null || userDto.getCaptchaKey() == null) {
            return new Result<>(ResultCode.CAPTCHA_ERROR);
        }
        if (!checkCaptcha(userDto.getCaptchaKey(), userDto.getCaptcha())) {
            return new Result<>(ResultCode.CAPTCHA_ERROR);
        }
        ResUser resUser = userService.login(userDto);
        if (resUser == null) {
            return new Result<>(ResultCode.PASSWORD_ERROR);
        }
        return new Result<>(resUser);
    }

    public boolean checkCaptcha(String captchaKey, String captcha) {
        String s = redisTemplate.opsForValue().get(UserConstant.VERIFICATION_CODE_KEY + ":" + captchaKey);
        System.out.println(s);
        if (s == null) {
            return false;
        }
        if (!captcha.equals(s)) {
            return false;
        }
        return true;
    }

}
